package top.xiaoazi.futureletterserver.module.letter.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.xiaoazi.futureletterserver.module.letter.common.LetterVariable;
import top.xiaoazi.futureletterserver.module.letter.entity.Letter;
import top.xiaoazi.futureletterserver.module.letter.entity.LetterBug;
import top.xiaoazi.futureletterserver.module.letter.mapper.LetterMapper;
import top.xiaoazi.futureletterserver.module.letter.vo.LetterCountOnCurUserVO;
import top.xiaoazi.futureletterserver.module.letter.vo.LetterSentVO;
import top.xiaoazi.futureletterserver.utils.CryptoAESUtils;
import top.xiaoazi.futureletterserver.utils.TimeUtils;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Service
public class LetterService {

    @Autowired
    private LetterMapper letterMapper;

    @Autowired
    private CryptoAESUtils cryptoAESUtils;

    public int send(Letter letter) {
        // 如果 letter 不存在 id 则直接发送
        if (letter.getId() == null) {
            return letterMapper.send(letter);
        } else {
            // 如果 letter 存在 id 表示是修改
            return letterMapper.modifySend(letter);
        }
    }

    public List<LetterSentVO> getSentInfo(Integer userId) {
        return letterMapper.getSentInfo(userId);
    }

    public List<LetterSentVO> getWaitingSendInfo(Integer userId) {
        return letterMapper.getWaitingSendInfo(userId);
    }

    public List<Letter> getSentAndPubInfo() {
        return letterMapper.getSentAndPubInfo();
    }

    public Letter getSentAndPubInfoById(Integer id) {
        return letterMapper.getSentAndPubInfoById(id);
    }

    public LetterSentVO getSendInfoById(Integer userId, Integer id) {
        // 获取Letter
        LetterSentVO letterSentVO = letterMapper.getSendInfoById(userId, id);
        return letterSentVO;
    }

    /**
     * 获取当天等待发送的信件
     * @return
     */
    public List<LetterSentVO> getAwaitSendLetterOnToday(long startTime, long endTime) {
        return letterMapper.getAwaitSendLetterOnToday(startTime, endTime);
    }

    /**
     * 设置信件的状态
     */
    public int setState(Letter letter) {
        return letterMapper.setState(letter);
    }

    /**
     * 统计当前登录的用户的信件数据统计
     * @param userId
     * @return
     */
    public LetterCountOnCurUserVO getCountOnCurUser(Integer userId) {
        // 统计已发出的
        int sentCount = letterMapper.getCountOnCurUserByState(userId, LetterVariable.SENT_STATE);
        // 统计未发出的
        int waitCount = letterMapper.getCountOnCurUserByState(userId, LetterVariable.WAIT_STATE);
        // 统计暂存箱的
        int saveCount = letterMapper.getCountOnCurUserByState(userId, LetterVariable.SAVE_STATE);
        // 统计公开的
        int pubCount = letterMapper.getCountOnCurUserByPub(userId, LetterVariable.PUB);

        LetterCountOnCurUserVO result = new LetterCountOnCurUserVO();
        result.setSentCount(sentCount);
        result.setWaitCount(waitCount);
        result.setSaveCount(saveCount);
        result.setPubCount(pubCount);
        return result;
    }

    public List<Letter> getSentAndPubInfoLimit(Integer pageSize, int offset) {
        return letterMapper.getSentAndPubInfoLimit(pageSize, offset);
    }

    /**
     * 获取暂存箱里面的数据
     * @param userId
     * @param params
     * @return
     * @throws Exception
     */
    public List<Letter> getSaveInfo(Integer userId, String params) throws Exception {
        List<Letter> letters = letterMapper.getSaveInfo(userId);
        if (params == null) {
            return letters;
        }
        // 由于是加密的数据, 所以只能在 service 里面进行解密之后, 进行模糊匹配查询
        // 把满足条件的数据放到 result 中
        List<Letter> result = new ArrayList<>();
        for (Letter letter : letters) {
            String title = cryptoAESUtils.decrypt(letter.getTitle());
            String content = cryptoAESUtils.decrypt(letter.getContent());
            if (title.contains(params) || content.contains(params)) {
                result.add(letter);
            }
        }
        return result;
    }

    /**
     * 判断该信是否是指定的用户写的
     * @param letterId
     * @param userId
     * @return
     */
    public boolean isLetterWrittenByUser(Integer letterId, Integer userId) {
        if (letterId == null || userId == null) {
            return false;
        }
        LetterSentVO letter = letterMapper.getSaveInfoById(letterId, userId);
        if (letter == null || letter.getId() == null || letter.getUserId() == null) {
            return false;
        }
        return true;
    }


    /**
     * 删除信件内容
     * @param letterId
     * @param userId
     * @return
     */
    public boolean delSaveInfo(Integer letterId, Integer userId) {
        if (!isLetterWrittenByUser(letterId, userId)) {
            return false;
        }
        // 当前写信人与信件id对应上，才可以执行删除
        letterMapper.delSaveInfo(letterId);
        return true;
    }

    /**
     * 通过letterid获取数据
     * @param letterId
     * @param userId
     * @return
     */
    public Letter getSaveInfoById(Integer letterId, Integer userId) {
        if (letterId == null || userId == null) {
            return null;
        }
        return letterMapper.getSaveInfoById(letterId, userId);
    }

    public List<LetterSentVO> getList(Integer userId) {
        // 拿到当前用户的已经发送的信件
        List<LetterSentVO> list1 = letterMapper.getSentInfo(userId);
        List<LetterSentVO> list2 = letterMapper.getWaitingSendInfo(userId);
        list1.addAll(list2);
        // 越新的时间排在前面
        list1.sort((o1, o2) -> o2.getWriteTime().compareTo(o1.getWriteTime()));
        return list1;
    }

    public int submitBug(LetterBug letterBug) {
        // 设置发送时间
        letterBug.setPushTime(new Timestamp(TimeUtils.currentTimeMillis()));
        letterBug.setSolve(false);
        return letterMapper.submitBug(letterBug);
    }

    public List<LetterBug> getBugs() {
        return letterMapper.getBugs();
    }

    public List<LetterBug> getBugsNoSolve() {
        return letterMapper.getBugsNoSolve();
    }

    public int getWaitingSendCount() {
        return letterMapper.getWaitingSendCount();
    }

    public int getSentCount() {
        return letterMapper.getSentCount();
    }

    public int changeLetterBug(LetterBug letterBug) {
        return letterMapper.changeLetterBug(letterBug);
    }


}
